package com.itheima.service.mongo.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserFreeze;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserFreezeService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.Duration;
import java.util.List;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName UserFreezeServiceImpl
 * @date 2022/11/10 22:33
 */
@DubboService
public class UserFreezeServiceImpl implements UserFreezeService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public String save(UserFreeze userFreeze) {
        // 判断userFreeze是否为空
        if (userFreeze.getUserId() != null) {
            // 判断redis中是否有freeze:userFreeze.getUserId()
            if (!stringRedisTemplate.hasKey("freeze:" + userFreeze.getUserId())) {
                // 如果存在就根据前端传的冻结时间状态进行设置冻结时间
                switch (userFreeze.getFreezingTime()){
                    case 1:
                        stringRedisTemplate.opsForValue().set("freeze:" + userFreeze.getUserId(),"2_1", Duration.ofDays(1));
                        break;
                    case 2:
                        stringRedisTemplate.opsForValue().set("freeze:" + userFreeze.getUserId(),"2_2", Duration.ofDays(7));
                        break;
                    case 3:
                        stringRedisTemplate.opsForValue().set("freeze:" + userFreeze.getUserId(),"2_3");
                        break;
                }
                // 创建条件查询器
                Query query = new Query(Criteria.where("userId").is(userFreeze.getUserId()).and("freezeStatus").is(1).and("freezingRange").is(userFreeze.getFreezingRange()));
                UserFreeze one = mongoTemplate.findOne(query, UserFreeze.class);
                if (one != null) {
                    Update update = new Update();
                    update.set("freezeStatus",2);
                    mongoTemplate.updateFirst(query,update,UserFreeze.class);
                } else {
                    userFreeze.setFreezeStatus(2);
                    mongoTemplate.save(userFreeze);
                }
            }
        }
        return "操作成功";
    }

    // 用户解冻操作
    @Override
    public String unfreezes(UserFreeze userFreeze) {
        if (stringRedisTemplate.hasKey("freeze:" + userFreeze.getUserId())) {
            stringRedisTemplate.delete("freeze:" + userFreeze.getUserId());
            // 创建条件查询器
            Query query = new Query(Criteria.where("userId").is(userFreeze.getUserId()).and("freezeStatus").is(2));
            Update update = new Update();
            update.set("freezeStatus",1);
            update.set("frozenRemarks",userFreeze.getFrozenRemarks());
            mongoTemplate.updateFirst(query,update,UserFreeze.class);
        }
        return "操作成功";
    }

    // 查询userFreeze信息
    @Override
    public UserFreeze findById(Long userId) {
        // 创建查询条件
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query, UserFreeze.class);
    }

    @Override
    public List<UserFreeze> findByFreeze(Long userId) {
        // 创建查询条件
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserFreeze> userFreezes = mongoTemplate.find(query, UserFreeze.class);
        return userFreezes;
    }
}
